* NOTE: This setup file was based off of the one used by the Public Policty Forum automation and AI paper

set more off
capture cd "C:\Users\Benjamin Allen\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "C:\Users\balle\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "~\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"

* Load soft launch data
usespss "data/Public+Policy+Forum+automation+and+AI+survey+April+2019_June+18,+2019_12.33.sav", clear


********************************************************************************
* Basic cleaning
********************************************************************************

*------------------------------------------------------------------------------*
* Removing useless variables
*------------------------------------------------------------------------------*

* Drop useless variables that seem to have been added for each text field
drop POSTA0 POSTA1 POSTA2 POSTA3 POSTA4 POSTA5 POSTA6
drop VOTE_0 VOTE_1 VOTE_2 VOTE_3 VOTE_4 VOTE_5 VOTE_6
drop VOTE_8 VOTE_9 VOTE_A VOTE_B VOTE_C VOTE_D VOTE_E
drop IMPOR0 IMPOR1 IMPOR2 IMPOR3 IMPOR4 IMPOR5 IMPOR6
drop PARTR0 PARTR1 PARTR2 PARTR3 PARTR4 PARTR5 PARTR6
drop JOB_T0 JOB_T1 JOB_T2 JOB_T3 JOB_T4 JOB_T5 JOB_T6
drop JOB_D1 JOB_D2 JOB_D3 JOB_D4 JOB_D5 JOB_D6 // a couple of these have text that overflowed the TEXT variable
drop NEWS_0 NEWS_1 NEWS_2 NEWS_3 NEWS_4 NEWS_5 NEWS_6
drop ETHNI0	ETHNI1	ETHNI2	ETHNI3	ETHNI4	ETHNI5	ETHNI6	ETHNI8	ETHNI9	ETHNIA	ETHNIB	ETHNIC	ETHNID	ETHNIE
drop EMPLO0 EMPLO1 EMPLO2 EMPLO3 EMPLO4 EMPLO5 EMPLO6
drop OCCUP0 OCCUP1 OCCUP2 OCCUP3 OCCUP4 OCCUP5 OCCUP6

* We don't need these either
drop IPAddress RecipientLastName RecipientFirstName RecipientEmail ExternalReference LocationLatitude LocationLongitude DistributionChannel

*------------------------------------------------------------------------------*
* Removing previews
*------------------------------------------------------------------------------*

tab Status

lab li Status

drop if Status == 1

*------------------------------------------------------------------------------*
* Checking reasons for termination
*------------------------------------------------------------------------------*

tab term gc, m
* Looks pretty reasonable

*------------------------------------------------------------------------------*
* Checking for incomplete responses
*------------------------------------------------------------------------------*

tab Finished, m

*------------------------------------------------------------------------------*
* Keeping only good completes
*------------------------------------------------------------------------------*

destring gc, replace
keep if gc == 1

********************************************************************************
* Quality review
********************************************************************************

* Flagging low-quality responses first, then exporting a list of their IDs to be replaced, then dropping them

*------------------------------------------------------------------------------*
* Survey time - total minutes spent
*------------------------------------------------------------------------------*

destring Q_TotalDuration, replace

gen survey_minutes = Q_TotalDuration / 60

sum survey_minutes, detail

gen survey_minutes_capped_40 = survey_minutes
replace survey_minutes_capped_40 = 40 if survey_minutes_capped_40 > 40

hist survey_minutes_capped_40, width(1) freq xline(9.3) note("Red line is speeding cutoff")
graph export "data/soft launch duration hist with speeding cutoff.png", replace

* Qualtrics will set the speeding cutoff at 9 minutes, so I'll drop those speeders
gen speeder = 0
replace speeder = 1 if survey_minutes <= 9
* Just 17 people
* The rest were already removed by the speeding cutoff that was built into the survey after the soft launch

*------------------------------------------------------------------------------*
* Survey time - time spent on pages of text
*------------------------------------------------------------------------------*

* Time spent on that page by non-speeders
sum algorithmic_govt_Page_Submit, detail

gen algorithmic_govt_time_capped_300 = algorithmic_govt_Page_Submit
replace algorithmic_govt_time_capped_300 = 300 if algorithmic_govt_time_capped_300 > 300

hist algorithmic_govt_time_capped_300, start(0) width(10) freq xlab(0(30)300) title("Time spent on algorithmic government""acceptability page, seconds", size(small))
graph export "data/soft launch algorithmic_govt time spent.png", replace

gen alg_govt_speeder = 0
replace alg_govt_speeder = 1 if algorithmic_govt_Page_Submit < 30

tab alg_govt_speeder
* 20% of non-speeder responses spent less than 30 seconds on the algorithmic government acceptability page

*------------------------------------------------------------------------------*
* Responses to text questions
*------------------------------------------------------------------------------*

* OTHER text responses
list vote_choice_6_TEXT if vote_choice_6_TEXT != ""
* Wait, did we not include the Bloc or People's Party in the question?
* We did include People's Party, but not the Bloc. Oops! Fix that!

list vote_2015_5_TEXT if vote_2015_5_TEXT != ""
* Need to add the BQ to this as well!

list important_issues_11_TEXT if important_issues_11_TEXT != ""
list partrust_tech_change_6_TEXT if partrust_tech_change_6_TEXT != ""
list news_sources_8_TEXT if news_sources_8_TEXT != ""
list ethnicity_8_TEXT if ethnicity_8_TEXT != ""
list ethnicity_9_TEXT if ethnicity_9_TEXT != ""

* A few interesting responses:
/*
"Métis. It's not the same as First Nations "
"Please say Caucasian"
*/

list employment_9_TEXT if employment_9_TEXT != ""

* We might consider adding a "Disabled" category to employment?

list occupation_11_TEXT if occupation_11_TEXT != ""
*list XXXXXXXXXXX if XXXXXXXXXXXXX != ""

* Open text questions
list job_title
list job_desc



* Flagging low-quality text responses

gen low_quality_text = 0

replace low_quality_text = 1 if vote_2015_5_TEXT == "Fuck part"

* Low-quality responses to job_title
replace low_quality_text = 1 if job_title == "0000"
replace low_quality_text = 1 if job_title == "..."
replace low_quality_text = 1 if job_title == "haku"
replace low_quality_text = 1 if job_title == "...."
replace low_quality_text = 1 if job_title == "jump"
replace low_quality_text = 1 if job_title == "later"
replace low_quality_text = 1 if job_title == "Bitch"

* Second round of replacements for job_title
replace low_quality_text = 1 if job_title == "Nnn"
replace low_quality_text = 1 if job_title == "Yoo"
replace low_quality_text = 1 if job_title == "Rvf"


* Low-quality responses to job_desc
replace low_quality_text = 1 if job_desc == "Bla bla bla bla bla bla ." // will be dropped due to "Bitch" in job_title
replace low_quality_text = 1 if job_desc == "Hybrid is and yf7yfy7f7yf7yfy7f7yf7yfy7f7yf7yf7yf7yf7yf7yf7yf7yf7yf7yf7yf7yf7yf7yfy7"
replace low_quality_text = 1 if job_desc == "DUNNO"
replace low_quality_text = 1 if job_desc == "Work for pay dau"
replace low_quality_text = 1 if job_desc == "Good,d,lee.owkwkw."
replace low_quality_text = 1 if job_desc == "JBJGJBMNVMBMB"
replace low_quality_text = 1 if job_desc == "... .."
replace low_quality_text = 1 if job_desc == "No..........,,."
replace low_quality_text = 1 if job_desc == "I do stuff"
replace low_quality_text = 1 if job_desc == "No comment"
replace low_quality_text = 1 if job_desc == "aucun"
replace low_quality_text = 1 if job_desc == "kjy uy67 uy678 uy6t87"
replace low_quality_text = 1 if job_desc == "NOT USRE WHAT I DO TBH"
replace low_quality_text = 1 if job_desc == "It so cool"
replace low_quality_text = 1 if job_desc == "Mundane"
replace low_quality_text = 1 if job_desc == "Everyday"
replace low_quality_text = 1 if job_desc == "what ever the hell I want to do"
replace low_quality_text = 1 if job_desc == "SAD LIFE"
replace low_quality_text = 1 if job_desc == "tired"
replace low_quality_text = 1 if job_desc == "Bjbfkngbgjkbfunbh BhadBabyNique JKK was I gonna just had 𝒥𝒶𝓈𝓂𝒾𝓃𝑒 big fat nun boy nun bout be b"
replace low_quality_text = 1 if job_desc == "....."
replace low_quality_text = 1 if job_desc == "hakua"

* Second round of replacements for job_desc
replace low_quality_text = 1 if job_desc == "A job is like any other jobs."
replace low_quality_text = 1 if job_desc == "Make money"
replace low_quality_text = 1 if job_desc == "Thvfdgbxvvgvgbjuufghgujdrtghffghhuydtyhcddrhhgxdyhcdggd4yh"
replace low_quality_text = 1 if job_desc == "Thanks"
replace low_quality_text = 1 if job_desc == "vhurwyhe yegerhgerhergergerggergrgergerg"
replace low_quality_text = 1 if job_desc == "Sksksksmsksmsk xkxkxkxkxkxmxk"
replace low_quality_text = 1 if job_desc == "adawa"
replace low_quality_text = 1 if job_desc == "VJMFBIOJFIOEFJGVDFIUVNCVNC"
replace low_quality_text = 1 if job_desc == "…………….."
replace low_quality_text = 1 if job_desc == "dvdfvdf fdvdfvd dfvdfv"
replace low_quality_text = 1 if job_desc == "N/SDFUYHISK"
replace low_quality_text = 1 if job_desc == "Hvfcttgrcffrv ggghghhhgfrvt:hhhjjjkk u ggggtgttt:evveevrbtjtnrnrjrr k 4j4benwbwnemr k r 4 krkejej3kemem3jekejejeh4hrh46c e yctvuvutvibt t moonunbytv 4 crccrtbybu n i 8 cyfbffnjgfgfefjjgcsvtjopoiyrrbfsnydvhtfgevgdvresvgkligfddnjjgrc ggfdgeb bdcc"


tab low_quality_text

*------------------------------------------------------------------------------*
* Straightlining
*------------------------------------------------------------------------------*

/*
* Message testing
gen message_straightlining = 0
replace message_straightlining = 1 if ///
	message_1 == message_2 & ///
	message_1 == message_3 & ///
	message_1 == message_4 & ///
	message_1 == message_5 & ///
	message_1 == message_6 & ///
	message_1 == message_7 & ///
	message_1 == message_8 & ///
	message_1 == message_9
*/

gen straightliner_vote_scale = 0
replace straightliner_vote_scale = 1 if ///
	vote_scale_1 == vote_scale_2 & ///
	vote_scale_1 == vote_scale_3 & ///
	vote_scale_1 == vote_scale_4 & ///
	vote_scale_1 == vote_scale_5
	
gen straightliner_leader_approval = 0
replace straightliner_leader_approval = 1 if ///
	leader_approval_1 == leader_approval_2 & ///
	leader_approval_1 == leader_approval_3 & ///
	leader_approval_1 == leader_approval_4 & ///
	leader_approval_1 == leader_approval_5

gen straightliner_job_class = 0
replace straightliner_job_class = 1 if ///
	job_instructions == job_sense & ///
	job_instructions == job_emotions & ///
	job_instructions == job_common_patterns & ///
	job_instructions == job_new_patterns & ///
	job_instructions == job_logic & ///
	job_instructions == job_planning & ///
	job_instructions == job_creative & ///
	job_instructions == job_search & ///
	job_instructions == job_collaborate & ///
	job_instructions == job_relationships & ///
	job_instructions == job_writing & ///
	job_instructions == job_communication & ///
	job_instructions == job_hands & ///
	job_instructions == job_lift & ///
	job_instructions == job_navigation & ///
	job_instructions == job_move ///

gen straightliner_positions = 0
replace straightliner_positions = 1 if ///
	pos_freetrade == pos_ustrade & ///
	pos_freetrade == pos_immigration & ///
	pos_freetrade == pos_automation_govt & ///
	pos_freetrade == pos_automation_cons & ///
	pos_freetrade == pos_automation_corp

gen straightliner_alg_govt = 0
replace straightliner_alg_govt = 1 if ///
	alg_govt_acceptable_1 == alg_govt_acceptable_2 & ///
	alg_govt_acceptable_1 == alg_govt_acceptable_3 & ///
	alg_govt_acceptable_1 == alg_govt_acceptable_4 & ///
	alg_govt_acceptable_1 == alg_govt_acceptable_5 & ///
	alg_govt_acceptable_1 == alg_govt_acceptable_6 & ///
	alg_govt_acceptable_1 == alg_govt_acceptable_7 & ///
	alg_govt_acceptable_1 == alg_govt_acceptable_8
	
gen straightliner_populism = 0
replace straightliner_populism = 1 if ///
	populism_1 == populism_2 & ///
	populism_1 == populism_3 & ///
	populism_1 == populism_4 & ///
	populism_1 == populism_5 & ///
	populism_1 == populism_6 & ///
	populism_1 == populism_7
	
gen straightliner_nativism = 0
replace straightliner_nativism = 1 if ///
	nativism_1 == nativism_2 & ///
	nativism_1 == nativism_3 & ///
	nativism_1 == nativism_4 & ///
	nativism_1 == nativism_5 & ///
	nativism_1 == nativism_6

sum straightliner_vote_scale straightliner_leader_approval straightliner_job_class straightliner_positions straightliner_alg_govt straightliner_populism straightliner_nativism
	
egen straightliner_total = rowtotal(straightliner_vote_scale straightliner_leader_approval straightliner_job_class straightliner_positions straightliner_alg_govt straightliner_populism straightliner_nativism)

tab straightliner_total

* Classify as straightliner if they straigntlined for 2+ questions
gen straightliner = 0
replace straightliner = 1 if straightliner_total >= 2


*------------------------------------------------------------------------------*
* Checking for coherent answers to the automation questions
*------------------------------------------------------------------------------*

* Setting automation values to missing if they are incoherent

* This is ASSUMING that automation of one's job is more likely in a longer timespan

* This really shouldn't get many responses

* creating and populating an incoherency flag
gen automation_incoherent = 0

*--------------------------------------*
* Personal automation threat
*--------------------------------------*

* If they think that losing their job to automation is LESS likely within 10 years than it is within 5 years
replace automation_incoherent = 1 if automation_personal_2 < automation_personal_1
* If they think that losing their job to automation is LESS likely within 25 years than it is within 5 years
replace automation_incoherent = 1 if automation_personal_3 < automation_personal_1
* If they think that losing their job to automation is LESS likely within 25 years than it is within 10 years
replace automation_incoherent = 1 if automation_personal_3 < automation_personal_2

*--------------------------------------*
* Prosocial automation threat
*--------------------------------------*

* If they think that people they know losing their job to automation is LESS likely within 10 years than it is within 5 years
replace automation_incoherent = 1 if automation_prosocial_2 < automation_prosocial_1
* If they think that people they know losing their job to automation is LESS likely within 25 years than it is within 5 years
replace automation_incoherent = 1 if automation_prosocial_3 < automation_prosocial_1
* If they think that people they know losing their job to automation is LESS likely within 25 years than it is within 10 years
replace automation_incoherent = 1 if automation_prosocial_3 < automation_prosocial_2

*--------------------------------------*
* Labelling
*--------------------------------------*

lab var automation_incoherent "Checking coherency of automation responses: if automation gets LESS likely over a longer period of time"
tab automation_incoherent


*--------------------------------------*
* Setting incoherent responses to zero (at least)
*--------------------------------------*

* If their response was incoherent, set all their automation responses to missing

* Personal
replace automation_personal_1 = . if automation_incoherent == 1
replace automation_personal_2 = . if automation_incoherent == 1
replace automation_personal_3 = . if automation_incoherent == 1

* Prosocial
replace automation_prosocial_1 = . if automation_incoherent == 1
replace automation_prosocial_2 = . if automation_incoherent == 1
replace automation_prosocial_3 = . if automation_incoherent == 1


*------------------------------------------------------------------------------*
* Checking for coherent answers to the expected employment questions
*------------------------------------------------------------------------------*

* Setting automation values to missing if they are incoherent

* This is ASSUMING that automation of one's job is more likely in a longer timespan

* This really shouldn't get many responses

* creating and populating an incoherency flag
gen employment_incoherent = 0


* If they they say that they will be working at their current employer in 5 years, but not in 3 years
replace employment_incoherent = 1 if employment_expect_2 == "Yes":employment_expect_2 & (employment_expect_1 == "No":employment_expect_1 | employment_expect_1 == "I will be retired":employment_expect_1)

* If they they say that they will be working at their current employer in 10 years, but not in 5 years
replace employment_incoherent = 1 if employment_expect_3 == "Yes":employment_expect_3 & (employment_expect_2 == "No":employment_expect_2 | employment_expect_2 == "I will be retired":employment_expect_2)

* If they they say that they will be working at their current employer in 10 years, but not in 3 years
replace employment_incoherent = 1 if employment_expect_3 == "Yes":employment_expect_3 & (employment_expect_1 == "No":employment_expect_1 | employment_expect_1 == "I will be retired":employment_expect_1)

*--------------------------------------*
* Labelling
*--------------------------------------*

lab var employment_incoherent "Checking coherency of employment responses: if not employed < employed"
tab employment_incoherent

tab employment_incoherent

*--------------------------------------*
* Setting incoherent responses to zero (at least)
*--------------------------------------*

* If their response was incoherent, set all their automation responses to missing

replace employment_expect_1 = . if employment_incoherent == 1
replace employment_expect_2 = . if employment_incoherent == 1
replace employment_expect_3 = . if employment_incoherent == 1



*------------------------------------------------------------------------------*
* Making a list of low-quality responses to replace
*------------------------------------------------------------------------------*

gen replace_respondent = 0
replace replace_respondent = 1 if ///
	speeder == 1 | ///
	low_quality_text == 1 | ///
	straightliner == 1 | ///
	automation_incoherent == 1 | ///
	employment_incoherent == 1
	
tab replace_respondent
	
preserve

keep if replace_respondent == 1
keep ResponseId rid replace_respondent speeder low_quality_text straightliner automation_incoherent employment_incoherent

export delimited "data/list of respondents to replace 3.csv", replace

restore

*------------------------------------------------------------------------------*
* Dropping low-quality responses
*------------------------------------------------------------------------------*

drop if replace_respondent == 1

********************************************************************************
* Recoding
********************************************************************************

*------------------------------------------------------------------------------*
* Language
*------------------------------------------------------------------------------*

* There were some responses from Quebec that were obviously from francophones

* Looking at the language they enter text into to determine their preferred language
list job_desc if province == "Quebec":province

replace Q_Language = "FR" if job_desc == "paysagiste"
replace Q_Language = "FR" if job_desc == "Je fais la finition de béton pour les plancher garage et balcons"
replace Q_Language = "FR" if job_desc == "Je m’occupe de commander la marchandise et les fournitures pour tous les départements de la ville de Saint-Jérôme. J’effectue la création de devis, demande de prix, négociations, réception de facture et paiement de facture, etc"
replace Q_Language = "FR" if job_desc == "Secrétaire service à la clientèle"
replace Q_Language = "FR" if job_desc == "Combler les besoins physiques ,phychologiques,développer l`autonomie,éduquer"
replace Q_Language = "FR" if job_desc == "None rien"

*------------------------------------------------------------------------------*
* Age
*------------------------------------------------------------------------------*

*create an "age" variable from yob.
gen age_in_years = 2019-(1919+yob)
replace age_in_years = . if yob == 82 //82 == "Don't know"
lab var age_in_years "Age in years"

count if age < 18
* 0, good

hist age_in_years, start(15) width(5) freq
* That looks reasonable

gen age_cat = ""
replace age_cat = "18-24" if age_in_years >= 18 & age_in_years <= 24
replace age_cat = "25-34" if age_in_years >= 25 & age_in_years <= 34
replace age_cat = "35-44" if age_in_years >= 35 & age_in_years <= 44
replace age_cat = "45-54" if age_in_years >= 45 & age_in_years <= 54
replace age_cat = "55-64" if age_in_years >= 55 & age_in_years <= 64
replace age_cat = "65+" if age_in_years >= 65

tab age_cat, m

*------------------------------------------------------------------------------*
* Female
*------------------------------------------------------------------------------*

lab li gender

gen female = 0
replace female = 1 if gender == 1

lab def female 0 "Not female" 1 "Female"
lab val female female

lab var female "Female (0/1)"


*------------------------------------------------------------------------------*
* Education
*------------------------------------------------------------------------------*

lab li education

* Set DKs to missing
replace education = . if education == 9

lab var education "Education (0-8)"

*------------------------------------------------------------------------------*
* Employment
*------------------------------------------------------------------------------*

lab li employment

gen employed = 0
replace employed = 1 if ///
	employment == 1 | /// 1 Employed part-time
    employment == 2 | /// 2 Employed full-time
    employment == 3 /// 3 Self-employed

lab def employed 0 "Not employed" 1 "Employed"
lab val employed employed
	
lab var employed "Employed (0/1)"	
	
tab employed

*------------------------------------------------------------------------------*
* Income
*------------------------------------------------------------------------------*

lab li income

replace income = . if income == 10
replace income = . if income == 11

lab var income "Income category (1-9)"

*------------------------------------------------------------------------------*
* Vote choice
*------------------------------------------------------------------------------*

* Respondents who are certain not to vote did not receive this question

/*
Display This Question: 
If In the next federal election, are you certain to vote, likely, unlikely, or certain not to vote? != Certain not to vote
 
vote_choice 
If a federal election were held tomorrow, which political party would you be most likely to vote for?
o	Green Party  (1) 
o	Liberal Party  (2) 
o	Conservative Party  (3) 
o	New Democratic Party  (4) 
o	Bloc Quebecois  (8) 
o	People’s Party  (5) 
o	Another party (please specify)  (6) ________________________________________________
o	Not Sure / Undecided  (7) 
*/

codebook vote_choice

lab def vote_choice 1 "Green Party" 2 "Liberal Party" 3 "Conservative Party" 4 "NDP" 5 "People's Party" 6 "Another party" 7 "Not sure / Undecided" 8 "Bloc Quebecois", replace

tab vote_choice

* Re-ordered version, with a more inuitive order
recode vote_choice ///
	(2 = 1 "Liberal Party") ///
	(3 = 2 "Conservative Party") ///
	(4 = 3 "New Democratic Party") ///
	(1 = 4 "Green Party") ///
	(8 = 5 "Bloc Quebecois") ///
	(5 = 6 "People's Party") ///
	(6 = 7 "Another party") ///
	(7 = 8 "Not sure / Undecided") ///
	, gen(vote_choice_reordered) lab(vote_choice_reordered)

tab vote_choice_reordered

*------------------------------------------------------------------------------*
* Left-Right scale
*------------------------------------------------------------------------------*

codebook lr_scale_1

rename lr_scale_1 lr_scale
lab var lr_scale "Left-Right self placement (0-10)"

replace lr_scale = 5 if lr_scale == .

*------------------------------------------------------------------------------*
* Fixing broken partrust_tech_change labels
*------------------------------------------------------------------------------*

tab partrust_tech_change
* Well that's kinda broken

lab li partrust_tech_change
* But that looks fine?

* Fixing this
lab def partrust_tech_change_FIXED ///
	1 "Green Party" ///
	2 "Liberal Party" ///
	3 "Conservative Party" ///
	4 "New Democratic Party" ///
	9 "Bloc Quebecois" ///
	5 "People’s Party" ///
	6 "Another party (please specify)" ///
	7 "I don't trust any party to manage technological change" ///
	8 "Don't know"
lab val partrust_tech_change partrust_tech_change_FIXED

tab partrust_tech_change

* Re-ordered version, with a more inuitive order
recode partrust_tech_change ///
	(2 = 1 "Liberal Party") ///
	(3 = 2 "Conservative Party") ///
	(4 = 3 "New Democratic Party") ///
	(1 = 4 "Green Party") ///
	(9 = 5 "Bloc Quebecois") ///
	(5 = 6 "People's Party") ///
	(6 = 7 "Another party") ///
	(7 = 8 "I don't trust any party to manage technological change") ///
	(8 = 9 "Don't know") ///
	, gen(partrust_tech_change_reordered) lab(partrust_tech_change_reordered)

tab partrust_tech_change_reordered

*------------------------------------------------------------------------------*
* Job classification Qs - changing to binary
*------------------------------------------------------------------------------*

recode job_instructions job_sense job_emotions job_common_patterns job_new_patterns job_logic job_planning job_creative job_search job_collaborate job_relationships job_writing job_communication job_hands job_lift job_navigation job_move ///
	(1 = 1 "Yes") ///
	(2 3 = 0 "No") ///
	/// (3 = . ) ///
	, generate(job_instructions_binary job_sense_binary job_emotions_binary job_common_patterns_binary job_new_patterns_binary job_logic_binary job_planning_binary job_creative_binary job_search_binary job_collaborate_binary job_relationships_binary job_writing_binary job_communication_binary job_hands_binary job_lift_binary job_navigation_binary job_move_binary) ///
	label(job_class_binary)

*------------------------------------------------------------------------------*
* Automation & AI inequality
*------------------------------------------------------------------------------*	
/*
AI_inequality Please indicate whether you agree or disagree with the following statement:
 
 Automation and artificial intelligence will make economic inequality worse in the future, with the rich getting richer and the poor getting poorer.  
o	Strongly disagree  (8) 
o	Disagree  (9) 
o	Neither agree nor disagree  (10) 
o	Agree  (11) 
o	Strongly agree  (12) 
o	Don't know  (13) 
*/

lab var AI_inequality "Automation and artificial intelligence will make economic inequality worse in the future, with the rich getting richer and the poor getting poorer."

recode AI_inequality ///
	(8  = 1 "Strongly disagree") ///
	(9  = 2 "Disagree") ///
	(10 = 3 "Neither agree nor disagree") ///
	(11 = 4 "Agree") ///
	(12 = 5 "Strongly agree") ///
	(13 = . ) /// "Don't know"
	, gen(AI_inequality_recoded) lab(AI_inequality_recoded)

*------------------------------------------------------------------------------*
* Automation & AI social mobility
*------------------------------------------------------------------------------*	
/*
AI_social_mobilitiy In the future, will automation and artificial intelligence make it easier for poor people to become richer, or harder?  
o	Easier  (1) 
o	Harder  (2) 
o	Not sure  (3) 

*/

gen AI_social_mobilitiy_recoded = AI_social_mobilitiy
lab val AI_social_mobilitiy_recoded AI_social_mobilitiy
replace AI_social_mobilitiy_recoded = . if AI_social_mobilitiy  == "Not sure":AI_social_mobilitiy 

*------------------------------------------------------------------------------*
* News volume
*------------------------------------------------------------------------------*

lab li news_volume

* Set DKs to missing
replace news_volume = . if news_volume == 7

lab var news_volume "News consumption (1-6)"

*------------------------------------------------------------------------------*
* Self-reported general knowledge
*------------------------------------------------------------------------------*

/*
AI_knowledge How much would you say you understand automation and artificial intelligence?  
o	I know nothing about it  (31) 
o	I have heard the words, but I don't understand them well  (32) 
o	I am familiar with the concepts, but I don't know much about them  (33) 
o	I have a basic understanding  (34) 
o	I have a good understanding  (35) 
o	I am an expert on automation and AI  (36) 
*/

lab li AI_knowledge

tab AI_knowledge

recode AI_knowledge ///
	(31 = 1 "I know nothing about it") ///
	(32 = 2 "I have heard the words, but I don't understand them well") ///
	(33 = 3 "I am familiar with the concepts, but I don't know much about them") ///
	(34 = 4 "I have a basic understanding") ///
	(35 = 5 "I have a good understanding") ///
	(36 = 6 "I am an expert on automation and AI") ///
	, gen(AI_knowledge_recoded) lab(AI_knowledge_recoded)

lab var AI_knowledge_recoded "Reported knowledge of automation and AI (1-6)"
	
tab AI_knowledge_recoded



*------------------------------------------------------------------------------*
* Current sector trends
*------------------------------------------------------------------------------*

/*
AI_current_knowledge Thinking about the industry or sector that you work in (or have most recently worked in), do you understand how automation and AI are currently changing your sector?  
o	Yes  (1) 
o	No  (2) 
o	Not sure  (4) 
*/

lab li AI_current_knowledge

tab AI_current_knowledge

recode AI_current_knowledge ///
	(2 = 1 "No") ///
	(4 = 2 "Not sure") ///
	(1 = 3 "Yes") ///
	, gen(AI_current_knowledge_recoded) lab(AI_current_knowledge_recoded)

lab var AI_current_knowledge_recoded "Current industry knowledge (1/2/3)"
	
tab AI_current_knowledge_recoded

*------------------------------------------------------------------------------*
* Future sector trends
*------------------------------------------------------------------------------*


/*
AI_future_knowledge Thinking about the industry or sector that you work in (or have most recently worked in), do you understand how automation and AI will change your sector in the future?  
o	Yes  (1) 
o	No  (2) 
o	Not sure  (3) 
*/

lab li AI_future_knowledge

tab AI_future_knowledge

recode AI_future_knowledge ///
	(2 = 1 "No") ///
	(3 = 2 "Not sure") ///
	(1 = 3 "Yes") ///
	, gen(AI_future_knowledge_recoded) lab(AI_future_knowledge_recoded)

lab var AI_future_knowledge_recoded "Future industry knowledge (1/2/3)"
	
tab AI_future_knowledge_recoded


*------------------------------------------------------------------------------*
* Relabel positions to exclude instructions
*------------------------------------------------------------------------------*

codebook pos_freetrade pos_ustrade pos_immigration pos_automation_govt pos_automation_cons pos_automation_corp pos_freetrade2 pos_ustrade2 pos_immigration2

lab var pos_freetrade "There should be more free trade with other countries, even if it hurts some industries in Canada."
lab var pos_freetrade2 "[2nd asking] There should be more free trade with other countries, even if it hurts some industries in Canada."

lab var pos_ustrade "I think free trade with the United States and Mexico has been good for Canada."
lab var pos_ustrade2 "[2nd asking] I think free trade with the United States and Mexico has been good for Canada."

lab var pos_immigration "Canada should increase the number of immigrants it admits each year."
lab var pos_immigration2 "[2nd asking] Canada should increase the number of immigrants it admits each year."

lab var pos_automation_govt "The government should penalize companies that fire workers and replace them with computers or machines."

lab var pos_automation_cons "Consumers should boycott companies that fire their employees and replace them with computers or machines."

lab var pos_automation_corp "Companies should continue to employ workers even when there are computers or machines available that could do their job more efficiently."

codebook pos_freetrade pos_ustrade pos_immigration pos_automation_govt pos_automation_cons pos_automation_corp pos_freetrade2 pos_ustrade2 pos_immigration2


*------------------------------------------------------------------------------*
* Relabel policy solutions to be shorter
*------------------------------------------------------------------------------*

*-------------------------------------------*
* Decrease immigration of skilled workers
*-------------------------------------------*
/*
policy_sol_skilled 
Which of the following statements do you agree with more?
o	Automation and artificial intelligence are going to take away a large number of jobs, so the federal government should begin decreasing immigration of skilled workers from other countries.   (1) 
o	I don’t think there is much the federal government can do to stop automation and artificial intelligence taking away a large number of jobs. Workers should prepare themselves for the changes that are coming.  (4) 
*/

lab li policy_sol_skilled

tab policy_sol_skilled

recode policy_sol_skilled ///
	(1 = 1 "Decrease skilled worker immigration") ///
	(4 = 0 "Can't do much") ///
	, gen(policy_sol_skilled_recoded) lab(policy_sol_skilled_recoded)

tab policy_sol_skilled_recoded

*-------------------------------------------*
* Decrease immigration of unskilled workers
*-------------------------------------------*
/*
policy_sol_unskilled 
Which of the following statements do you agree with more?
o	Automation and artificial intelligence are going to take away a large number of jobs, so the federal government should begin decreasing immigration of unskilled workers from other countries.   (1) 
o	I don’t think there is much the federal government can do to stop automation and artificial intelligence taking away a large number of jobs. Workers should prepare themselves for the changes that are coming.  (4) 
*/

lab li policy_sol_unskilled

tab policy_sol_unskilled

recode policy_sol_unskilled ///
	(1 = 1 "Decrease unskilled worker immigration") ///
	(4 = 0 "Can't do much") ///
	, gen(policy_sol_unskilled_recoded) lab(policy_sol_unskilled_recoded)

tab policy_sol_unskilled_recoded

*-------------------------------------------*
* Spend more on STEM university education
*-------------------------------------------*
/*
policy_sol_education 
Which of the following statements do you agree with more?
o	Automation and artificial intelligence are going to take away a large number of jobs, so the federal government should begin spending a lot more money on university education in science and technology.  (1) 
o	I don’t think there is much the federal government can do to stop automation and artificial intelligence taking away a large number of jobs. Workers should prepare themselves for the changes that are coming.  (4) 
*/

lab li policy_sol_education

tab policy_sol_education

recode policy_sol_education ///
	(1 = 1 "Spend more on university education in STEM") ///
	(4 = 0 "Can't do much") ///
	, gen(policy_sol_education_recoded) lab(policy_sol_education_recoded)

tab policy_sol_education_recoded

*-------------------------------------------*
* Retrain older adults
*-------------------------------------------*
/*
policy_sol_retrain 
Which of the following statements do you agree with more?
o	Automation and artificial intelligence are going to take away a large number of jobs, so the federal government should begin spending a lot more money on retraining programs for older adults.  (1) 
o	I don’t think there is much the federal government can do to stop automation and artificial intelligence taking away a large number of jobs. Workers should prepare themselves for the changes that are coming.  (4) 
*/

lab li policy_sol_retrain

tab policy_sol_retrain

recode policy_sol_retrain ///
	(1 = 1 "Retrain older adults") ///
	(4 = 0 "Can't do much") ///
	, gen(policy_sol_retrain_recoded) lab(policy_sol_retrain_recoded)

tab policy_sol_retrain_recoded

*-------------------------------------------*
* Tax incentives for companies to retrain workers
*-------------------------------------------*
/*
policy_sol_tax_incen 
Which of the following statements do you agree with more?
o	Automation and artificial intelligence are going to take away a large number of jobs, so the federal government should create tax incentives for companies to retrain workers who are displaced by automation.  (1) 
o	I don’t think there is much the federal government can do to stop automation and artificial intelligence taking away a large number of jobs. Workers should prepare themselves for the changes that are coming.  (4) 
*/

lab li policy_sol_tax_incen

tab policy_sol_tax_incen

recode policy_sol_tax_incen ///
	(1 = 1 "Tax incentives for companies to retrain workers") ///
	(4 = 0 "Can't do much") ///
	, gen(policy_sol_tax_incen_recoded) lab(policy_sol_tax_incen_recoded)

tab policy_sol_tax_incen_recoded

*-------------------------------------------*
* More generous EI benefits for displaced
*-------------------------------------------*
/*
policy_sol_benefits 
Which of the following statements do you agree with more?
o	Automation and artificial intelligence are going to take away a large number of jobs, so the federal government should allow workers who are displaced by automation to claim more generous EI benefits.  (1) 
o	I don’t think there is much the federal government can do to stop automation and artificial intelligence taking away a large number of jobs. Workers should prepare themselves for the changes that are coming.  (4) 
*/

lab li policy_sol_benefits

tab policy_sol_benefits

recode policy_sol_benefits ///
	(1 = 1 "More generous EI benefits for displaced") ///
	(4 = 0 "Can't do much") ///
	, gen(policy_sol_benefits_recoded) lab(policy_sol_benefits_recoded)

tab policy_sol_benefits_recoded

*-------------------------------------------*
* Tax punishment for companies that displace
*-------------------------------------------*
/*
policy_sol_tax_pun 
Which of the following statements do you agree with more?
o	Automation and artificial intelligence are going to take away a large number of jobs, so the federal government should use the corporate tax system to punish companies who displace workers through automation.  (1) 
o	I don’t think there is much the federal government can do to stop automation and artificial intelligence taking away a large number of jobs. Workers should prepare themselves for the changes that are coming.  (4) 
*/

lab li policy_sol_tax_pun

tab policy_sol_tax_pun

recode policy_sol_tax_pun ///
	(1 = 1 "Tax punishment for companies that displace") ///
	(4 = 0 "Can't do much") ///
	, gen(policy_sol_tax_pun_recoded) lab(policy_sol_tax_pun_recoded)

tab policy_sol_tax_pun_recoded

*-------------------------------------------*
* Favour companies that haven't displaced
*-------------------------------------------*
/*
policy_sol_favour 
Which of the following statements do you agree with more?
o	Automation and artificial intelligence are going to take away a large number of jobs, so when selecting companies to provide the government with goods and services, the government should favour companies that haven’t laid off workers through automation.  (1) 
o	I don’t think there is much the federal government can do to stop automation and artificial intelligence taking away a large number of jobs. Workers should prepare themselves for the changes that are coming.  (4) 
*/

lab li policy_sol_favour

tab policy_sol_favour

recode policy_sol_favour ///
	(1 = 1 "Favour companies that haven't displaced") ///
	(4 = 0 "Can't do much") ///
	, gen(policy_sol_favour_recoded) lab(policy_sol_favour_recoded)

tab policy_sol_favour_recoded


codebook policy_sol_skilled_recoded ///
	policy_sol_unskilled_recoded ///
	policy_sol_education_recoded ///
	policy_sol_retrain_recoded ///
	policy_sol_tax_incen_recoded ///
	policy_sol_benefits_recoded ///
	policy_sol_tax_pun_recoded ///
	policy_sol_favour_recoded
	
*------------------------------------------------------------------------------*
* Policy solutions as percentages
*------------------------------------------------------------------------------*	
	
* Scaling up to percentages
gen policy_sol_skilled_recoded_pct = policy_sol_skilled_recoded*100
gen policy_sol_unskilled_recoded_pct = policy_sol_unskilled_recoded*100
gen policy_sol_education_recoded_pct = policy_sol_education_recoded*100
gen policy_sol_retrain_recoded_pct = policy_sol_retrain_recoded*100
gen policy_sol_tax_incen_recoded_pct = policy_sol_tax_incen_recoded*100
gen policy_sol_benefits_recoded_pct = policy_sol_benefits_recoded*100
gen policy_sol_tax_pun_recoded_pct = policy_sol_tax_pun_recoded*100
gen policy_sol_favour_recoded_pct = policy_sol_favour_recoded*100

lab var policy_sol_skilled_recoded_pct "Decrease skilled worker immigration"
lab var policy_sol_unskilled_recoded_pct "Decrease unskilled worker immigration"
lab var policy_sol_education_recoded_pct "Spend more on STEM university education"
lab var policy_sol_retrain_recoded_pct "Retrain older adults"
lab var policy_sol_tax_incen_recoded_pct `""Tax incentives for companies to""retrain workers""'
lab var policy_sol_benefits_recoded_pct "More generous EI benefits for displaced workers"
lab var policy_sol_tax_pun_recoded_pct `""Tax punishment for companies that""displace workers""'
lab var policy_sol_favour_recoded_pct `""Favour companies that haven\'t displaced""workers for government procurement""'

*------------------------------------------------------------------------------*
* Employment length
*------------------------------------------------------------------------------*

codebook employment_length

recode employment_length ///
	( . = 0 "Not employed") ///
	(92 = 1 "Less than 6 months") ///
	(93 = 2 "6 months to 1 year") ///
	(94 = 3 "2 to 3 years") ///
	(95 = 4 "3 to 5 years") ///
	(96 = 5 "6 to 10 years") ///
	(97 = 6 "More than 10 years") ///
	, gen(employment_length_recoded) lab(employment_length_recoded)

lab var employment_length_recoded "Length of current employment (0-6)"
	
tab employment_length_recoded, m

*------------------------------------------------------------------------------*
* Financial security
*------------------------------------------------------------------------------*

codebook secure_normal secure_shock secure_retirement

recode secure_normal ///
	(4 = 1 "Strongly disagree") ///
	(5 = 2 "Disagree") ///
	(6 = 3 "Neither agree nor disagree") ///
	(7 = 4 "Agree") ///
	(8 = 5 "Strongly agree") ///
	(9 = . ) /// "Don't know"
	, gen(secure_normal_recoded) lab(secure_normal_recoded)

lab var secure_normal_recoded "Normal expense financial security (1-5)"
	
recode secure_shock ///
	(4 = 1 "Strongly disagree") ///
	(5 = 2 "Disagree") ///
	(6 = 3 "Neither agree nor disagree") ///
	(7 = 4 "Agree") ///
	(8 = 5 "Strongly agree") ///
	(9 = . ) /// "Don't know"
	, gen(secure_shock_recoded) lab(secure_shock_recoded)

lab var secure_shock_recoded "Unexpected cost financial security (1-5)"
	
recode secure_retirement ///
	(4 = 1 "Strongly disagree") ///
	(5 = 2 "Disagree") ///
	(6 = 3 "Neither agree nor disagree") ///
	(7 = 4 "Agree") ///
	(8 = 5 "Strongly agree") ///
	(9 = . ) /// "Don't know"
	, gen(secure_retirement_recoded) lab(secure_retirement_recoded)

lab var secure_retirement_recoded "Retirement financial security (1-5)"

*------------------------------------------------------------------------------*
* Skills in current economy
*------------------------------------------------------------------------------*

/*
skills_current Please indicate whether you agree or disagree with the following statement:

I feel I have the skills necessary to maintain my standard of living in the current economy.
o	Strongly disagree  (4) 
o	Disagree  (5) 
o	Neither agree nor disagree  (6) 
o	Agree  (7) 
o	Strongly agree  (8) 
o	Don't know  (9) 
*/

codebook skills_current

recode skills_current ///
	(4 = 1 "Strongly disagree") ///
	(5 = 2 "Disagree") ///
	(6 = 3 "Neither agree nor disagree") ///
	(7 = 4 "Agree") ///
	(8 = 5 "Strongly agree") ///
	(9 = . ) /// "Don't know"
	, gen(skills_current_recoded) lab(skills_current_recoded)

lab var skills_current_recoded "Confidence in current skills (1-5)"
	
*------------------------------------------------------------------------------*
* Skills in future economy
*------------------------------------------------------------------------------*

/*
skills_future Please indicate whether you agree or disagree with the following statement:

I feel I have the skills necessary to maintain my standard of living in the economy five years from now.
o	Strongly disagree  (4) 
o	Disagree  (5) 
o	Neither agree nor disagree  (6) 
o	Agree  (7) 
o	Strongly agree  (8) 
o	Don't know  (9) 
*/

codebook skills_future

recode skills_future ///
	(4 = 1 "Strongly disagree") ///
	(5 = 2 "Disagree") ///
	(6 = 3 "Neither agree nor disagree") ///
	(7 = 4 "Agree") ///
	(8 = 5 "Strongly agree") ///
	(9 = . ) /// "Don't know"
	, gen(skills_future_recoded) lab(skills_future_recoded)	

lab var skills_future_recoded "Confidence in skills for future (1-5)"


*------------------------------------------------------------------------------*
* Acceptability of algorithmic governance
*------------------------------------------------------------------------------*

/*

Acceptable reason to use an algorithm (1)
Unacceptable reason to use an algorithm (2)
Not sure (3)

To reduce the time required to make decisions. (1) 
To make decisions which will be a better use of government money. (2) 
To make sure decisions are not influenced by factors like a program recipient's gender, ethnicity, or wealth. (3) 
To make sure decisions are not influenced by officials’ biases. (4) 
To reduce fraud against the government. (5) 
To make decisions which are more consistent and less “random”. (6) 
To reduce the number of bureaucrats/government officials. (7) 
To reduce the costs of government. (8) 


*/

* Orderinal version
recode alg_govt_acceptable_1 alg_govt_acceptable_2 alg_govt_acceptable_3 alg_govt_acceptable_4 alg_govt_acceptable_5 alg_govt_acceptable_6 alg_govt_acceptable_7 alg_govt_acceptable_8 ///
	(1 = 1 "Acceptable") ///
	(2 = -1 "Unacceptable") ///
	(3 = 0 "Not sure") ///
	, gen(	alg_govt_acceptable_1_ord ///
			alg_govt_acceptable_2_ord ///
			alg_govt_acceptable_3_ord ///
			alg_govt_acceptable_4_ord ///
			alg_govt_acceptable_5_ord ///
			alg_govt_acceptable_6_ord ///
			alg_govt_acceptable_7_ord ///
			alg_govt_acceptable_8_ord ///
			) ///
	lab(alg_govt_acceptable_ord)

* Binary version
recode alg_govt_acceptable_1 alg_govt_acceptable_2 alg_govt_acceptable_3 alg_govt_acceptable_4 alg_govt_acceptable_5 alg_govt_acceptable_6 alg_govt_acceptable_7 alg_govt_acceptable_8 ///
	(1 = 1 "Acceptable") ///
	(2 = 0 "Unacceptable") ///
	(3 = 0 "Not sure") ///
	, gen(	alg_govt_acceptable_1_bi ///
			alg_govt_acceptable_2_bi ///
			alg_govt_acceptable_3_bi ///
			alg_govt_acceptable_4_bi ///
			alg_govt_acceptable_5_bi ///
			alg_govt_acceptable_6_bi ///
			alg_govt_acceptable_7_bi ///
			alg_govt_acceptable_8_bi ///
			) ///
	lab(alg_govt_acceptable_bi)

********************************************************************************
* Weighting
********************************************************************************

*------------------------------------------------------------------------------*
* Basic demographic weight
*------------------------------------------------------------------------------*
* I copied the code from the CES weight



*------------------------------------------------------------*
* Country level weights
*------------------------------------------------------------*

*--------------------------------------*
* Creating appropriate variables where required
*--------------------------------------*

* Province weighting targets:
/*

Newfoundland and Labrador	0.015299208
Prince Edward Island		0.004104979
Nova Scotia					0.027094536
New Brunswick				0.02183925
Quebec (English)			0.02068478
Quebec (French)				0.214008701
Ontario						0.383972876
Manitoba					0.035132598
Saskatchewan				0.030022282
Alberta						0.11212364
British Columbia			0.135717149

*/

lab li province

* I need to create a new province_language variable, so that I can distinguish between EN and FR Quebeckers. Might as well follow the East-to-West ordering I used above
gen province_language = .
replace province_language = 1 if province == "Newfoundland and Labrador":province
replace province_language = 2 if province == "Prince Edward Island":province
replace province_language = 3 if province == "Nova Scotia":province
replace province_language = 4 if province == "New Brunswick":province
replace province_language = 5 if province == "Quebec":province & Q_Language == "EN"
replace province_language = 6 if province == "Quebec":province & Q_Language == "FR"
replace province_language = 7 if province == "Ontario":province
replace province_language = 8 if province == "Manitoba":province
replace province_language = 9 if province == "Saskatchewan":province
replace province_language = 10 if province == "Alberta":province
replace province_language = 11 if province == "British Columbia":province

tab province_language, m

* Age category
/*

18-24	0.109411298
25-34	0.163945587
35-44	0.161476287
45-54	0.179123898
55-64	0.174674059
65+		0.211368871

*/

gen age_cat_num = .
replace age_cat_num = 1 if age_cat == "18-24"
replace age_cat_num = 2 if age_cat == "25-34"
replace age_cat_num = 3 if age_cat == "35-44"
replace age_cat_num = 4 if age_cat == "45-54"
replace age_cat_num = 5 if age_cat == "55-64"
replace age_cat_num = 6 if age_cat == "65+"


* Gender
lab li gender
/*

Men		0.485702206
Women	0.514300647

*/

gen gender_binary = .
replace gender_binary = 0 if gender == "Male":gender
replace gender_binary = 1 if gender == "Female":gender

*--------------------------------------*
* Set up matrices
*--------------------------------------*

* Required variable for the ipf matrices 
generate byte _one = 1

* Country level weighting targets

* Province
matrix province_language_weight = (0.015299208, 0.004104979, 0.027094536, 0.02183925, 0.02068478, 0.214008701, 0.383972876, 0.035132598, 0.030022282, 0.11212364, 0.135717149)
matrix colnames province_language_weight = 1 2 3 4 5 6 7 8 9 10 11
matrix coleq province_language_weight = _one
matrix rownames province_language_weight = province_language

* Country level age
matrix age_cat_weight = (0.109411298, 0.163945587, 0.161476287, 0.179123898, 0.174674059, 0.211368871)
matrix colnames age_cat_weight = 1 2 3 4 5 6
matrix coleq age_cat_weight = _one
matrix rownames age_cat_weight = age_cat_num

* Country level gender
matrix gender_weight = (0.485702206, 0.514300647)
matrix colnames gender_weight = 0 1
matrix coleq gender_weight = _one
matrix rownames gender_weight = gender_binary


*------------------------------------------------------------*
* Dynamically scale the weighting to the number of observations
*------------------------------------------------------------*

* The previous version uses 37422 - we will have to use a local variable to do this dynamically

count

* trimhiabs = 5 / # of respondents
local trimhiabs_num = 5 / r(N)

* trimhiabs = 0.2 / # of respondents
local trimloabs_num = 0.2 / r(N)

di `trimhiabs_num'
di  `trimloabs_num'

*--------------------------------------*
* Run weights
*--------------------------------------*

* Note that we're not using lower or upper bounds on these weights, for the example

* old weight - required for the ipfraking code, we set this to 1 for all respondents 
gen weight_one = 1 

set more off

ipfraking [pw = weight_one], gen(quota_weight) ctotal(province_language_weight age_cat_weight gender_weight) iterate(200) trimhiabs(`trimhiabs_num')  trimloabs(`trimloabs_num') nodivergence nograph

*  multiply by the number of respondents:
count
replace quota_weight = quota_weight * r(N)

** Dropping extra variables
drop weight_one
drop _one
drop province_language age_cat_num gender_binary

* Drop matrices
matrix drop _all

*--------------------------------------*
* Check weights and save
*--------------------------------------*

sum quota_weight

hist quota_weight, width(0.05) start(0.2) freq xline(1) xlabel(0.2(0.2)5, labsize(vsmall))
graph export "analysis/graphs/quota-based weight distribution.png", replace

********************************************************************************
* Saving results
********************************************************************************

save "data/Public+Policy+Forum+automation+and+AI+survey+April+2019_June+18,+2019_12.33 cleaned and weighted alg-sup-gov version.dta", replace

exit

********************************************************************************
* Reviewing results
********************************************************************************

*------------------------------------------------------------------------------*
* Intro demographics
*------------------------------------------------------------------------------*

tab citizenship 
tab province 
codebook postalcode 
codebook yob
tab gender

* So far, our sample is overwhelmingly female



hist age_in_years, start(15) width(5) freq
* That looks reasonable

*------------------------------------------------------------------------------*
* Voting, parties and leaders
*------------------------------------------------------------------------------*

tab vote_likelihood
tab vote_choice

sum vote_scale_1 vote_scale_2 vote_scale_3 vote_scale_4 vote_scale_5

* NOTE vote_scale_6 is for the BQ, and was added late

* Trudeau
tab leader_approval_1

* Scheer
tab leader_approval_2

* Singh
tab leader_approval_3

* May
tab leader_approval_4

* Bernier
tab leader_approval_5

tab lr_scale_1

tab vote_2015

tab pid

replace important_issues_1 = 0 if important_issues_1 == .
replace important_issues_2 = 0 if important_issues_2 == .
replace important_issues_3 = 0 if important_issues_3 == .
replace important_issues_4 = 0 if important_issues_4 == .
replace important_issues_5 = 0 if important_issues_5 == .
replace important_issues_6 = 0 if important_issues_6 == .
replace important_issues_7 = 0 if important_issues_7 == .
replace important_issues_8 = 0 if important_issues_8 == .
replace important_issues_9 = 0 if important_issues_9 == .
replace important_issues_10 = 0 if important_issues_10 == .
replace important_issues_11 = 0 if important_issues_11 == .

sum important_issues_1 important_issues_2 important_issues_3 important_issues_4 important_issues_5 important_issues_6 important_issues_7 important_issues_8 important_issues_9 important_issues_10 important_issues_11



* Trust party to deal with tech change
tab partrust_tech_change


*------------------------------------------------------------------------------*
* WVS items
*------------------------------------------------------------------------------*

sum tasks_manual_1 tasks_routine_1 tasks_independence_1

*------------------------------------------------------------------------------*
* Job classification
*------------------------------------------------------------------------------*

sum job_instructions_binary job_sense_binary job_emotions_binary job_common_patterns_binary job_new_patterns_binary job_logic_binary job_planning_binary job_creative_binary job_search_binary job_collaborate_binary job_relationships_binary job_writing_binary job_communication_binary job_hands_binary job_lift_binary job_navigation_binary job_move_binary

*------------------------------------------------------------------------------*
* automation and AI
*------------------------------------------------------------------------------*

* AI knowledge

codebook AI_knowledge AI_current_knowledge AI_future_knowledge

* Job loss due to automation
codebook automation_personal_1 automation_personal_2 automation_personal_3 automation_prosocial_1 automation_prosocial_2 automation_prosocial_3

* Fear of automation
codebook automation_fear

* Inequality
codebook AI_inequality 

* Social mobility
codebook AI_social_mobilitiy

* Positions
codebook pos_freetrade pos_ustrade pos_immigration pos_automation_govt pos_automation_cons pos_automation_corp


* Policy experiment
codebook policy_sol_skilled policy_sol_unskilled policy_sol_education policy_sol_retrain policy_sol_tax_incen policy_sol_benefits policy_sol_tax_pun policy_sol_favour

* Acceptable measures
codebook alg_govt_acceptable_1 alg_govt_acceptable_2 alg_govt_acceptable_3 alg_govt_acceptable_4 alg_govt_acceptable_5 alg_govt_acceptable_6 alg_govt_acceptable_7 alg_govt_acceptable_8

* Vingnettes
codebook alg_govt_imm alg_govt_tax alg_govt_loans

*------------------------------------------------------------------------------*
* Populism and nativism
*------------------------------------------------------------------------------*

* Populism
codebook populism_1 populism_2 populism_3 populism_4 populism_5 populism_6 populism_7

* Nativism
codebook nativism_1 nativism_2 nativism_3 nativism_4 nativism_5 nativism_6

*------------------------------------------------------------------------------*
* News
*------------------------------------------------------------------------------*

codebook news_sources_1 news_sources_2 news_sources_3 news_sources_4 news_sources_5 news_sources_6 news_sources_7 news_sources_8 news_sources_9 news_sources_10 news_sources_11

tab news_volume


*------------------------------------------------------------------------------*
* Mindwandering
*------------------------------------------------------------------------------*

* mindwandering_1 mindwandering_2 mindwandering_3 mindwandering_4 mindwandering_5 mindwandering_6 mindwandering_7 mindwandering_8


*------------------------------------------------------------------------------*
* Facts
*------------------------------------------------------------------------------*

* The baseline is 25% selecting the correct one
tab facts_alberta
tab facts_notredame
tab facts_sudan
tab facts_airplane
tab facts_shooting
tab facts_blackhole

* The proportions for these will be different
tab facts_carbontax_use
tab facts_carbontax_who

gen correct 

*------------------------------------------------------------------------------*
* More demographics
*------------------------------------------------------------------------------*

tab education
tab income

tab employment

tab occupation
list occupation_11_TEXT if occupation_11_TEXT != ""

*------------------------------------------------------------------------------*
* Additional labour market questions
*------------------------------------------------------------------------------*

tab employment_length
tab employment_expect_1 
tab employment_expect_2
tab employment_expect_3 
tab secure_normal
tab secure_shock
tab secure_retirement
tab skills_current
tab skills_future

*------------------------------------------------------------------------------*
* Experimental treatments
*------------------------------------------------------------------------------*

tab alg_govt_imm_decision
tab alg_govt_tax_decision
tab alg_govt_loans_decision
tab response_order_reversed

